package bookshop.service.impl;

import java.util.List;

import bookshop.dao.DaoFactory;
import bookshop.dao.ReceiveAddrDao;
import bookshop.domain.ReceiveAddr;
import bookshop.service.ReceiveAddrService;

public class ReceiveAddrServiceImpl implements ReceiveAddrService {

	public List<ReceiveAddr> GetAllReceiverAddrByCustId(String custId) {
		ReceiveAddrDao receiveAddrDao = (ReceiveAddrDao) DaoFactory
				.getDao("receiveAddrDao");
		return receiveAddrDao.GetAllReceiverAddrByCustId(custId);

	}

	public ReceiveAddr GetReceiverAddrById(int receiveId) {
		ReceiveAddrDao receiveAddrDao = (ReceiveAddrDao) DaoFactory
				.getDao("receiveAddrDao");
		return receiveAddrDao.GetReceiverAddrById(receiveId);
	}
	
	public void SavaReceiveAddr(ReceiveAddr receiveAddr){
		ReceiveAddrDao receiveAddrDao = (ReceiveAddrDao) DaoFactory
				.getDao("receiveAddrDao");
		List<ReceiveAddr> ras = receiveAddrDao.GetAllReceiverAddrByCustId(receiveAddr.getCustId());
		
		
		if(ras!=null){
		for (ReceiveAddr item:ras){
			if(item.getReceiverStatus().equals("Y")){
				item.setReceiverStatus("N");
				receiveAddrDao.UpdateReceiveAddr(item);
			}
			
		}
		}
		
		receiveAddrDao.AddReceiverAddr(receiveAddr);
	}
	
	public void UpdateReceiveAddr(ReceiveAddr ra){
		ReceiveAddrDao receiveAddrDao = (ReceiveAddrDao) DaoFactory
				.getDao("receiveAddrDao");
		receiveAddrDao.UpdateReceiveAddr(ra);
		
	}

	public ReceiveAddr GetReceiveAddrByStatus(String custId){
		ReceiveAddrDao receiveAddrDao = (ReceiveAddrDao) DaoFactory
				.getDao("receiveAddrDao");
		ReceiveAddr ra = new ReceiveAddr();
		for (ReceiveAddr item:receiveAddrDao.GetAllReceiverAddrByCustId(custId)){
			if(item.getReceiverStatus().equals("Y")){
				
				ra = item;
				break;
			}
		}
		return ra;
	}
	

}
